﻿namespace Graphix.Charts.Base
{
    using System.Collections.Generic;
    using System.Drawing;
    using Graphix.Elements;
    using Graphix.Helpers;
    using CommonHelpers;

    public abstract class BothAxisAndLegend<TV> : CoreWithLegend<TV>
    {
        #region Implementation of IChart

        public Axis XAxis { get; set; }

        public Axis YAxis { get; set; }

        protected override void CheckFonts()
        {
            base.CheckFonts();

            // XAxis
            this.XAxis.TextStyle = this.SetupFont(this.XAxis.TextStyle);
            this.XAxis.Title = this.SetupFont(this.XAxis.Title, this.TextStyle);

            // YAxis
            this.YAxis.TextStyle = this.SetupFont(this.YAxis.TextStyle);
            this.YAxis.Title = this.SetupFont(this.YAxis.Title, this.TextStyle);
        }

        protected void AddXAxisTitle()
        {
            if (this.XAxis.Title != null && !string.IsNullOrEmpty(this.XAxis.Title.Text))
            {
                PerformMove move;
                float position;
                StringAlignment alignment;

                if (this.XAxis.Title.Alignment == Horizontal.Right)
                {
                    move = PerformMove.Right;
                    position = this.Border.Right;
                    alignment = StringAlignment.Far;
                }
                else if (this.XAxis.Title.Alignment == Horizontal.Center)
                {
                    move = PerformMove.Left | PerformMove.Right | PerformMove.HalfX;
                    position = this.Border.Left + (this.Border.Width / 2f);
                    alignment = StringAlignment.Center;
                }
                else
                {
                    move = PerformMove.Left;
                    position = this.Border.Left;
                    alignment = StringAlignment.Near;
                }

                
                RectangleF measured = DefaultGraphics.Instance.MeasureDisplayString(this.XAxis.Title.Text, this.XAxis.Title.Font);
                var height = measured.Height * 1.1f;
                ChartString cs = new ChartString(position,
                    this.Border.Bottom - (measured.Height * 0.1f),
                    measured.Width, height, this.XAxis.Title.Text,
                    this.XAxis.Title.Font, this.XAxis.Title.ColorBrush);
                cs.Format = new StringFormat() { Alignment = alignment };

                cs.MoveWhen = move | PerformMove.Bottom;
                if (this.XAxis.DisplayValues)
                {
                    this.Border.Bottom -= height;
                }

                this.AddElement(cs);

                if (!this.XAxis.DisplayValues)
                {
                    this.Border.Bottom -= height;
                }
            }
        }

        protected void AddYAxisTitle()
        {
            if (this.YAxis.Title != null && !string.IsNullOrEmpty(this.YAxis.Title.Text))
            {
                PerformMove move;
                float position;
                StringAlignment alignment;

                if (this.YAxis.Title.Alignment == Horizontal.Right)
                {
                    move = PerformMove.Top;
                    position = this.Border.Top;
                    alignment = StringAlignment.Far;
                }
                else if (this.YAxis.Title.Alignment == Horizontal.Center)
                {
                    move = PerformMove.Top | PerformMove.Bottom | PerformMove.HalfY;
                    position = this.Border.Top + (this.Border.Height / 2);
                    alignment = StringAlignment.Center;
                }
                else
                {
                    move = PerformMove.Bottom;
                    position = this.Border.Bottom;
                    alignment = StringAlignment.Near;
                }

                RectangleF measured = DefaultGraphics.Instance.MeasureDisplayString(this.YAxis.Title.Text, this.YAxis.Title.Font);
                ChartString cs = new ChartString(this.Border.Left,
                    position,
                    measured.Width, measured.Height, this.YAxis.Title.Text,
                    this.YAxis.Title.Font, this.YAxis.Title.ColorBrush)
                {
                    Orientation = Orientation.Vertical,
                    Rotation = -90
                };
                cs.Format = new StringFormat { Alignment = alignment };

                cs.MoveWhen = move;

                this.Border.Left += measured.Height;

                this.AddElement(cs);
            }
        }
        protected List<string> SetsName { get; set; }

        public virtual void AddSeries(string name, List<TV> values)
        {
            Color usedColor = this.GetNextColor();
            if (!this.SetsName.Contains(name))
            {
                this.SetsName.Add(name);
            }

            this.values.Add(new ValueSet<TV>(usedColor, name, values));
        }

        public BothAxisAndLegend(Color[] possibleColors = null)
            : base(possibleColors)
        {
            this.XAxis = new Axis();
            this.YAxis = new Axis();

            this.SetsName = new List<string>();
        }
        #endregion
    }
}
